package com.ijianji.module_zhuanzhu.ui

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ConvertUtils
import com.fenghuajueli.libbasecoreui.utils.QuickClickUtils
import com.fenghuajueli.libbasecoreui.viewmodel.BaseViewModelFragment2
import com.fenghuajueli.module_route.ZhuanZhuModuleRoute
import com.ijianji.module_zhuanzhu.adapter.ZhuanZhuCategoryTabAdapter
import com.ijianji.module_zhuanzhu.adapter.ZhuanZhuTimeTabAdapter
import com.ijianji.module_zhuanzhu.databinding.FragmentZhuanZhuSettingBinding
import com.ijianji.module_zhuanzhu.model.ZhuanZhuModel
import com.warkiz.widget.IndicatorSeekBar
import com.warkiz.widget.OnSeekChangeListener
import com.warkiz.widget.SeekParams
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator

/**
 * 专注功能设置界面
 */
@Route(path = ZhuanZhuModuleRoute.FRAGMENT_ZHUANZHU_SETTING)
class ZhuanZhuSettingFragment : BaseViewModelFragment2<ZhuanZhuModel, FragmentZhuanZhuSettingBinding>() {


    override fun createViewModel(): ZhuanZhuModel {
        return ZhuanZhuModel()
    }

    override fun createViewBinding(): FragmentZhuanZhuSettingBinding {
        return FragmentZhuanZhuSettingBinding.inflate(layoutInflater)
    }

    override fun initView(containerView: View?) {
        initTab()
        initTimeTab()
        binding.seekBar.onSeekChangeListener = object : OnSeekChangeListener {
            override fun onSeeking(seekParams: SeekParams?) {
                seekParams?.let {
                    binding.tvMinute.text = "${seekParams.progress}"
                }

            }

            override fun onStartTrackingTouch(seekBar: IndicatorSeekBar?) {

            }

            override fun onStopTrackingTouch(seekBar: IndicatorSeekBar?) {

            }
        }
        binding.btnStart.setOnClickListener {
            if (QuickClickUtils.isFastClick()) {
                return@setOnClickListener
            }
            ARouter.getInstance().build(ZhuanZhuModuleRoute.ACTIVITY_ZHUANZHU_START)
                    .withInt("model", model.currentTypePosition)
                    .withInt("timemodel", model.currentTimePosition)
                    .withInt("duration", binding.seekBar.progress)
                    .navigation()
        }
    }

    override fun initData() {

    }

    fun initTab() {
        val tabAdapter = ZhuanZhuCategoryTabAdapter(binding.magicIndicator, arrayListOf("普通专注", "学霸", "考试"))
        tabAdapter.setOnListClickListener { position, data ->
            model.currentTypePosition = position
            if (position == 2) {
                binding.timeIndicator.visibility = View.GONE
                binding.seekBar.setProgress(90f)
            } else {
                binding.timeIndicator.visibility = View.VISIBLE
                if (model.getProgress() >= 0) {
                    binding.seekBar.setProgress(model.getProgress().toFloat())
                }
            }
            when (position) {
                0 -> {
                    binding.tvCategoryName.text = "普通专注"
                    binding.tvCategoryDesc.text = "适用于简单日常任务"
                }
                1 -> {
                    binding.tvCategoryName.text = "学霸模式"
                    binding.tvCategoryDesc.text = "高度集中注意力，抵制手机诱惑"
                }
                2 -> {
                    binding.tvCategoryName.text = "考试模式"
                    binding.tvCategoryDesc.text = "锻炼考试用时分配，掌握解题节奏"
                }
            }
        }
        val commonNavigator = CommonNavigator(context)
        commonNavigator.adapter = tabAdapter
        commonNavigator.leftPadding = ConvertUtils.dp2px(9f)
        commonNavigator.rightPadding = ConvertUtils.dp2px(9f)
        commonNavigator.isAdjustMode = true
        binding.magicIndicator.navigator = commonNavigator
    }


    fun initTimeTab() {
        val tabAdapter = ZhuanZhuTimeTabAdapter(binding.timeIndicator, arrayListOf("25min", "60min", "120min", "不限时"))
        tabAdapter.setOnListClickListener { position, data ->
            model.currentTimePosition = position
            if (position == 3) {
                binding.tvMinute.text = "不限时"
            } else {
                if (model.getProgress() >= 0) {
                    binding.seekBar.setProgress(model.getProgress().toFloat())
                }
            }
        }
        val commonNavigator = CommonNavigator(context)
        commonNavigator.adapter = tabAdapter
        commonNavigator.leftPadding = ConvertUtils.dp2px(9f)
        commonNavigator.rightPadding = ConvertUtils.dp2px(9f)
        commonNavigator.isAdjustMode = true
        binding.timeIndicator.navigator = commonNavigator
    }
}